home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / pc / SATAN11.ZIP / SRC / RPCGEN / RPC_HOUT.C < prev    next >
Encoding:
C/C++ Source or Header  |  1995-04-08  |  8.6 KB  |  382 lines

  1. /* @(#)rpc_hout.c    2.1 88/08/01 4.0 RPCSRC */
  2. /*
  3.  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
  4.  * unrestricted use provided that this legend is included on all tape
  5.  * media and as a part of the software program in whole or part.  Users
  6.  * may copy or modify Sun RPC without charge, but are not authorized
  7.  * to license or distribute it to anyone else except as part of a product or
  8.  * program developed by the user.
  9.  * 
  10.  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
  11.  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
  12.  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
  13.  * 
  14.  * Sun RPC is provided with no support and without any obligation on the
  15.  * part of Sun Microsystems, Inc. to assist in its use, correction,
  16.  * modification or enhancement.
  17.  * 
  18.  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
  19.  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
  20.  * OR ANY PART THEREOF.
  21.  * 
  22.  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
  23.  * or profits or other special, indirect and consequential damages, even if
  24.  * Sun has been advised of the possibility of such damages.
  25.  * 
  26.  * Sun Microsystems, Inc.
  27.  * 2550 Garcia Avenue
  28.  * Mountain View, California  94043
  29.  */
  30. #ifndef lint
  31. static char sccsid[] = "@(#)rpc_hout.c 1.6 87/07/28 (C) 1987 SMI";
  32. #endif
  33.  
  34. /*
  35.  * rpc_hout.c, Header file outputter for the RPC protocol compiler 
  36.  * Copyright (C) 1987, Sun Microsystems, Inc. 
  37.  */
  38. #include <stdlib.h>
  39. #include <unistd.h>
  40. #include <stdio.h>
  41. #include <ctype.h>
  42. #include "rpc_util.h"
  43. #include "rpc_parse.h"
  44.  
  45. static pconstdef();
  46. static pstructdef();
  47. static puniondef();
  48. static pdefine();
  49. static pprogramdef();
  50. static penumdef();
  51. static ptypedef();
  52. static pdeclaration();
  53. static undefined2();
  54.  
  55. /*
  56.  * Print the C-version of an xdr definition 
  57.  */
  58. void
  59. print_datadef(def)
  60.     definition *def;
  61. {
  62.     if (def->def_kind != DEF_CONST) {
  63.         f_print(fout, "\n");
  64.     }
  65.     switch (def->def_kind) {
  66.     case DEF_STRUCT:
  67.         pstructdef(def);
  68.         break;
  69.     case DEF_UNION:
  70.         puniondef(def);
  71.         break;
  72.     case DEF_ENUM:
  73.         penumdef(def);
  74.         break;
  75.     case DEF_TYPEDEF:
  76.         ptypedef(def);
  77.         break;
  78.     case DEF_PROGRAM:
  79.         pprogramdef(def);
  80.         break;
  81.     case DEF_CONST:
  82.         pconstdef(def);
  83.         break;
  84.     }
  85.     if (def->def_kind != DEF_PROGRAM && def->def_kind != DEF_CONST) {
  86.         f_print(fout, "bool_t xdr_%s();\n", def->def_name);
  87.     }
  88.     if (def->def_kind != DEF_CONST) {
  89.         f_print(fout, "\n");
  90.     }
  91. }
  92.  
  93. static
  94. pconstdef(def)
  95.     definition *def;
  96. {
  97.     pdefine(def->def_name, def->def.co);
  98. }
  99.  
  100. static
  101. pstructdef(def)
  102.     definition *def;
  103. {
  104.     decl_list *l;
  105.     char *name = def->def_name;
  106.  
  107.     f_print(fout, "struct %s {\n", name);
  108.     for (l = def->def.st.decls; l != NULL; l = l->next) {
  109.         pdeclaration(name, &l->decl, 1);
  110.     }
  111.     f_print(fout, "};\n");
  112.     f_print(fout, "typedef struct %s %s;\n", name, name);
  113. }
  114.  
  115. static
  116. puniondef(def)
  117.     definition *def;
  118. {
  119.     case_list *l;
  120.     char *name = def->def_name;
  121.     declaration *decl;
  122.  
  123.     f_print(fout, "struct %s {\n", name);
  124.     decl = &def->def.un.enum_decl;
  125.     if (streq(decl->type, "bool")) {
  126.         f_print(fout, "\tbool_t %s;\n", decl->name);
  127.     } else {
  128.         f_print(fout, "\t%s %s;\n", decl->type, decl->name);
  129.     }
  130.     f_print(fout, "\tunion {\n");
  131.     for (l = def->def.un.cases; l != NULL; l = l->next) {
  132.         pdeclaration(name, &l->case_decl, 2);
  133.     }
  134.     decl = def->def.un.default_decl;
  135.     if (decl && !streq(decl->type, "void")) {
  136.         pdeclaration(name, decl, 2);
  137.     }
  138.     f_print(fout, "\t} %s_u;\n", name);
  139.     f_print(fout, "};\n");
  140.     f_print(fout, "typedef struct %s %s;\n", name, name);
  141. }
  142.  
  143.  
  144.  
  145. static
  146. pdefine(name, num)
  147.     char *name;
  148.     char *num;
  149. {
  150.     f_print(fout, "#define %s %s\n", name, num);
  151. }
  152.  
  153. static
  154. puldefine(name, num)
  155.     char *name;
  156.     char *num;
  157. {
  158.     f_print(fout, "#define %s ((u_long)%s)\n", name, num);
  159. }
  160.  
  161. static
  162. define_printed(stop, start)
  163.     proc_list *stop;
  164.     version_list *start;
  165. {
  166.     version_list *vers;
  167.     proc_list *proc;
  168.  
  169.     for (vers = start; vers != NULL; vers = vers->next) {
  170.         for (proc = vers->procs; proc != NULL; proc = proc->next) {
  171.             if (proc == stop) {
  172.                 return (0);
  173.             } else if (streq(proc->proc_name, stop->proc_name)) {
  174.                 return (1);
  175.             }
  176.         }
  177.     }
  178.     abort();
  179.     /* NOTREACHED */
  180. }
  181.  
  182.  
  183. static
  184. pprogramdef(def)
  185.     definition *def;
  186. {
  187.     version_list *vers;
  188.     proc_list *proc;
  189.  
  190.     puldefine(def->def_name, def->def.pr.prog_num);
  191.     for (vers = def->def.pr.versions; vers != NULL; vers = vers->next) {
  192.         puldefine(vers->vers_name, vers->vers_num);
  193.         for (proc = vers->procs; proc != NULL; proc = proc->next) {
  194.             if (!define_printed(proc, def->def.pr.versions)) {
  195.                 puldefine(proc->proc_name, proc->proc_num);
  196.             }
  197.             pprocdef(proc, vers);
  198.         }
  199.     }
  200. }
  201.  
  202.  
  203. pprocdef(proc, vp)
  204.     proc_list *proc;
  205.     version_list *vp;
  206. {
  207.     f_print(fout, "extern ");
  208.     if (proc->res_prefix) {
  209.         if (streq(proc->res_prefix, "enum")) {
  210.             f_print(fout, "enum ");
  211.         } else {
  212.             f_print(fout, "struct ");
  213.         }
  214.     }
  215.     if (streq(proc->res_type, "bool")) {
  216.         f_print(fout, "bool_t *");
  217.     } else if (streq(proc->res_type, "string")) {
  218.         f_print(fout, "char **");
  219.     } else {
  220.         f_print(fout, "%s *", fixtype(proc->res_type));
  221.     }
  222.     pvname(proc->proc_name, vp->vers_num);
  223.     f_print(fout, "();\n");
  224. }
  225.  
  226. static
  227. penumdef(def)
  228.     definition *def;
  229. {
  230.     char *name = def->def_name;
  231.     enumval_list *l;
  232.     char *last = NULL;
  233.     int count = 0;
  234.  
  235.     f_print(fout, "enum %s {\n", name);
  236.     for (l = def->def.en.vals; l != NULL; l = l->next) {
  237.         f_print(fout, "\t%s", l->name);
  238.         if (l->assignment) {
  239.             f_print(fout, " = %s", l->assignment);
  240.             last = l->assignment;
  241.             count = 1;
  242.         } else {
  243.             if (last == NULL) {
  244.                 f_print(fout, " = %d", count++);
  245.             } else {
  246.                 f_print(fout, " = %s + %d", last, count++);
  247.             }
  248.         }
  249.         f_print(fout, ",\n");
  250.     }
  251.     f_print(fout, "};\n");
  252.     f_print(fout, "typedef enum %s %s;\n", name, name);
  253. }
  254.  
  255. static
  256. ptypedef(def)
  257.     definition *def;
  258. {
  259.     char *name = def->def_name;
  260.     char *old = def->def.ty.old_type;
  261.     char prefix[8];    /* enough to contain "struct ", including NUL */
  262.     relation rel = def->def.ty.rel;
  263.  
  264.  
  265.     if (!streq(name, old)) {
  266.         if (streq(old, "string")) {
  267.             old = "char";
  268.             rel = REL_POINTER;
  269.         } else if (streq(old, "opaque")) {
  270.             old = "char";
  271.         } else if (streq(old, "bool")) {
  272.             old = "bool_t";
  273.         }
  274.         if (undefined2(old, name) && def->def.ty.old_prefix) {
  275.             s_print(prefix, "%s ", def->def.ty.old_prefix);
  276.         } else {
  277.             prefix[0] = 0;
  278.         }
  279.         f_print(fout, "typedef ");
  280.         switch (rel) {
  281.         case REL_ARRAY:
  282.             f_print(fout, "struct {\n");
  283.             f_print(fout, "\tu_int %s_len;\n", name);
  284.             f_print(fout, "\t%s%s *%s_val;\n", prefix, old, name);
  285.             f_print(fout, "} %s", name);
  286.             break;
  287.         case REL_POINTER:
  288.             f_print(fout, "%s%s *%s", prefix, old, name);
  289.             break;
  290.         case REL_VECTOR:
  291.             f_print(fout, "%s%s %s[%s]", prefix, old, name,
  292.                 def->def.ty.array_max);
  293.             break;
  294.         case REL_ALIAS:
  295.              f_print(fout, "%s%s %s", prefix, old, name);
  296.             break;
  297.         }
  298.         f_print(fout, ";\n");
  299.     }
  300. }
  301.  
  302.  
  303. static
  304. pdeclaration(name, dec, tab)
  305.     char *name;
  306.     declaration *dec;
  307.     int tab;
  308. {
  309.     char buf[8];    /* enough to hold "struct ", include NUL */
  310.     char *prefix;
  311.     char *type;
  312.  
  313.     if (streq(dec->type, "void")) {
  314.         return;
  315.     }
  316.     tabify(fout, tab);
  317.     if (streq(dec->type, name) && !dec->prefix) {
  318.         f_print(fout, "struct ");
  319.     }
  320.     if (streq(dec->type, "string")) {
  321.         f_print(fout, "char *%s", dec->name);
  322.     } else {
  323.         prefix = "";
  324.         if (streq(dec->type, "bool")) {
  325.             type = "bool_t";
  326.         } else if (streq(dec->type, "opaque")) {
  327.             type = "char";
  328.         } else {
  329.             if (dec->prefix) {
  330.                 s_print(buf, "%s ", dec->prefix);
  331.                 prefix = buf;
  332.             }
  333.             type = dec->type;
  334.         }
  335.         switch (dec->rel) {
  336.         case REL_ALIAS:
  337.             f_print(fout, "%s%s %s", prefix, type, dec->name);
  338.             break;
  339.         case REL_VECTOR:
  340.             f_print(fout, "%s%s %s[%s]", prefix, type, dec->name,
  341.                 dec->array_max);
  342.             break;
  343.         case REL_POINTER:
  344.             f_print(fout, "%s%s *%s", prefix, type, dec->name);
  345.             break;
  346.         case REL_ARRAY:
  347.             f_print(fout, "struct {\n");
  348.             tabify(fout, tab);
  349.             f_print(fout, "\tu_int %s_len;\n", dec->name);
  350.             tabify(fout, tab);
  351.             f_print(fout, "\t%s%s *%s_val;\n", prefix, type, dec->name);
  352.             tabify(fout, tab);
  353.             f_print(fout, "} %s", dec->name);
  354.             break;
  355.         }
  356.     }
  357.     f_print(fout, ";\n");
  358. }
  359.  
  360.  
  361.  
  362. static
  363. undefined2(type, stop)
  364.     char *type;
  365.     char *stop;
  366. {
  367.     list *l;
  368.     definition *def;
  369.  
  370.     for (l = defined; l != NULL; l = l->next) {
  371.         def = (definition *) l->val;
  372.         if (def->def_kind != DEF_PROGRAM) {
  373.             if (streq(def->def_name, stop)) {
  374.                 return (1);
  375.             } else if (streq(def->def_name, type)) {
  376.                 return (0);
  377.             }
  378.         }
  379.     }
  380.     return (1);
  381. }
  382.